package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;
import jdk.nashorn.internal.scripts.JD;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public int add(ProductModel productModel) {
        String sql = "insert into product (name,img_href,price,normal_price,cuisine_id,content) values(?,?,?,?,?,?)";
        List<Object> list = new ArrayList<>();
        list.add(productModel.getName());
        list.add(productModel.getImgHref());
        list.add(productModel.getPrice());
        list.add(productModel.getNormalPrice());
        list.add(productModel.getCuisineId());
        list.add(productModel.getContent());
        return JDBC.update(sql, list);
    }

    @Override
    public int update(ProductModel productModel) {
        return 0;
    }

    @Override
    public int delete(ProductModel productModel) {
        return 0;
    }

    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
        String sql = "select p.*,c.name cuisineName from product p\n" +
                "inner join cuisine c\n" +
                "on p.cuisine_id=c.id ";
        sql += " limit  " + (productModel.getCurPage() - 1) * +productModel.getPageSize() + "," + productModel.getPageSize() + "";

        return getList(sql);
    }

    private List<ProductModel> getList(String sql) {
        List<ProductModel> list = new ArrayList<>();
        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                ProductModel model = new ProductModel();
                model.setId(query.getInt("id"));
                model.setName(query.getString("name"));
                model.setPrice(query.getFloat("price"));
                model.setNormalPrice(query.getDouble("normal_price"));
                model.setContent(query.getString("content"));
                model.setIsShow(query.getInt("is_show"));
                model.setEnable(query.getInt("enable"));
                model.setImgHref(query.getString("img_href"));
                model.setCuisineName(query.getString("cuisineName"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public ProductModel findById(ProductModel productModel) {
        return null;
    }

    @Override
    public int count(ProductModel productModel) {
        String sql = "select count(*) count from product p\n" +
                "inner join cuisine c\n" +
                "on p.cuisine_id=c.id  ";

        ResultSet query = JDBC.query(sql);
        try {
            while (query.next()) {
                return query.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
